<?php
require_once('../includes/settings.php');
require_once('../includes/functions.php');
require_once('../includes/repository.php');
require_once('../includes/sql.php');
deny_get_request();
if(isset($_GET['action'])){

	if($_GET['action'] == 'check_code' && isset($_POST['code']) ){
		@session_start();
		if( $_SESSION['security_code'] == strtolower($_POST['code']) ){
			return _json('true');
		}
		_json("null");
	}

}else{

	$name = $_POST['name'];
	$email = trim(strtolower($_POST['email']));
	$password = $_POST['password'];
	$code = $_POST['code'];
	@session_start();
	if($_SESSION['security_code'] == strtolower($code)){
		if(User::is_exist($email)){
			_json('{"success":false,"error_code":1}');
			exit();
		}else{
			$user_id = User::create($name,$password,$email);
			UserConfig::create($user_id, AMOUNT_PER_DAY, INIT_MONEY);
			Repository::commit();
			Repository::close();
			if($user_id > 0){
				$_SESSION['user']=array('id'=>$user_id,'name'=>$name,'email'=>$email,'first_time'=>TRUE);
				$_SESSION['user_confg']= array('amount_per_day' => AMOUNT_PER_DAY, 'money'=>INIT_MONEY);
				_json('{"success":true}');
			}else{
				_json('{"success":false}');
			}
		}
	}
	exit();
}

?>